package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.pojo.MarketBrand;
import com.cskaoyan.bean.vo.BrandListVO;
import com.cskaoyan.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 后台-品牌制造商模块
 * 创建日期: 2022/04/06 21:28
 *
 * @author HanWeiqi
 */
@RequestMapping("admin")
@RestController
public class MarketBrandController {
    @Autowired
    BrandService brandService;

    // 品牌制造商：/admin/brand/list ----------------------------------
    @RequestMapping("brand/list")
    public BaseRespVo brandList(BaseParam baseParam,Integer id,String name){
        BrandListVO brandListVO = brandService.brandList(baseParam,id,name);
        return BaseRespVo.ok(brandListVO);
    }

    // 品牌制造商：/admin/brand/create ----------------------------------
    @PostMapping("brand/create")
    public BaseRespVo brandCreate(@RequestBody @Validated MarketBrand marketBrand, BindingResult result){

        if(result.hasFieldErrors()){
            return BaseRespVo.fail(401, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }

        int count = brandService.brandCreate(marketBrand);

        if(count == 1){
            return BaseRespVo.ok();
        }else{
            return BaseRespVo.fail(10000,"创建失败");
        }

    }


    // 品牌制造商：/admin/brand/update ----------------------------------
    @PostMapping("brand/update")
    public BaseRespVo brandUpdate(@RequestBody @Validated MarketBrand marketBrand, BindingResult result){

        if(result.hasFieldErrors()){
            return BaseRespVo.fail(401, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }

        int count = brandService.brandUpdate(marketBrand);

        if(count == 1){
            return BaseRespVo.ok();
        }else{
            return BaseRespVo.fail(10000,"创建失败");
        }

    }

    // 品牌制造商：/admin/brand/delete ----------------------------------
    @PostMapping("brand/delete")
    public BaseRespVo brandDelete(@RequestBody MarketBrand marketBrand){

        int count = brandService.brandDelete(marketBrand);
        if(count == 1){
            return BaseRespVo.ok();
        }else{
            return BaseRespVo.fail(10000,"删除失败");
        }

    }

}
